/*
 * 人人信用管理有限公司版权所有.
 * 
 * @Description:
 * 
 * @author: liyuliang
 * 
 * @date: 20 Apr 2017 10:44:16 am
 * 
 * 注意：本内容仅限于人人信用管理有限公司内部传阅，禁止外泄以及用于其他的商业目的.
 */

package com.rrx360.common.service.impl;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Date;

import org.apache.commons.lang.time.DateUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.rrx360.common.exception.UserException;
import com.rrx360.common.pagequery.FieldEntry;
import com.rrx360.common.pagequery.PageQueryCondition;
import com.rrx360.common.pagequery.PageQueryResult;
import com.rrx360.common.service.BaseBeanService;

public class BaseBeanServiceImpl<T> implements BaseBeanService<T> {

  protected Logger logger = LogManager.getLogger(this.getClass());

  protected Class<T> beanClass;

  public BaseBeanServiceImpl() {
    /**
     * get the bean class by reflection
     */
    Type superType = getClass().getGenericSuperclass();
    if (superType instanceof ParameterizedType) {
      Type[] types = ((ParameterizedType) superType).getActualTypeArguments();
      if (types.length == 1) {
        beanClass = (Class<T>) types[0];
      }
    }
  }

  public T getBeanById(Serializable id) {
    return null;
  }

  public void addBean(T bean) {

  }

  public void updateBean(T bean) {

  }

  public void deleteBean(Serializable[] ids) {
    if (ids != null) {
      for (Serializable id : ids) {
        deleteBean(id);
      }
    }
  }

  public void deleteBean(Serializable id) {

  }


  public PageQueryResult findByPage(PageQueryCondition condition) {
    return null;
  }

  protected void setDateQueryValue(FieldEntry entry) {
    if (entry == null || entry.getValue() == null || "".equals(entry.getValue())) {
      return;
    }
    try {
      Date date = DateUtils.parseDate((String) entry.getValue(), new String[] {"yyyy-MM-dd"});
      entry.setQueryValue(date);
    } catch (Exception e) {
      if (logger.isDebugEnabled()) {
        logger.error(e.getMessage(), e);
      }
      throw new UserException("日期格式错误");
    }
  }
  
}
